package week3;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

/**
 * @author The Setting sun
 * @create 2021-09-23 15:09
 */
public class work2 {
    public static void main(String[] args) {
        String[] province = new String[]{"请选择", "湖南", "广东", "浙江", "上海"};
        String[][] city = new String[][]{{"请选择"},
                {"长沙", "株洲", "湘潭"},
                {"广州", "东莞", "深圳", "佛山"},
                {"杭州", "温州", "嘉兴"},
                {"上海"}};

        JFrame frame = new JFrame();
        frame.setLayout(new FlowLayout());

        JLabel label1 = new JLabel("省份：");
        label1.setFont(new Font("微软雅黑", Font.BOLD, 18));
        JComboBox box1 = new JComboBox(province);
        box1.setFont(new Font("微软雅黑", Font.BOLD, 18));

        JLabel label2 = new JLabel("城市：");
        label2.setFont(new Font("微软雅黑", Font.BOLD, 18));
        JComboBox box2 = new JComboBox(city[0]);
        box2.setFont(new Font("微软雅黑", Font.BOLD, 18));

        box1.addItemListener(new ItemListener() {
            @Override
            public void itemStateChanged(ItemEvent e) {
                int index = box1.getSelectedIndex();
                box2.removeAllItems();
                for (int i = 0; i < city[index].length; i++) {
                    box2.addItem(city[index][i]);
                }
            }
        });

        frame.add(label1);
        frame.add(box1);
        frame.add(label2);
        frame.add(box2);

        frame.setSize(600, 500);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}
